package com.jt.tenant.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jt.tenant.R;
import com.jt.tenant.app.JTApplication;
import com.jt.tenant.dialog.MyTimePopup;
import com.zcx.helper.activity.AppV4Fragment;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import static com.jt.tenant.fragment.OrderRefuelFragment.orderRefuelRefreshListener;

/**
 * Created by AD on 2016/5/12.
 */
public class OrderFragment extends AppV4Fragment implements OnClickListener {

    private LinearLayout lin_order_all,lin_order_refuel,lin_order_maintain,lin_order_insurance;

    private View view_all, view_refuel, view_maintain, view_insurance;

    private TextView tv_time;

    private ImageView iv_left, iv_right;

//    private MyOrder1Fragment myOrder1Fragment;
//
//    private MyOrder2Fragment myOrder2Fragment;
//
//    private MyOrder3Fragment myOrder3Fragment;

    private OrderRefuelFragment orderRefuelFragment;

    private android.support.v4.app.FragmentManager fragmentManager;

    private Calendar calendar = Calendar.getInstance();

    private MyTimePopup myTimePopup;

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");

    private static String time;

    private int frag_id;

    private int on_off;

    private int show = 4;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = JTApplication.ScaleScreenHelper.loadView((ViewGroup) inflater.inflate(R.layout.fragment_order, null));

        fragmentManager = getChildFragmentManager();

        time = simpleDateFormat.format(calendar.getTime());

//        view_all = (View)view.findViewById(R.id.view_all);

        view_refuel = (View)view.findViewById(R.id.view_refuel);

        view_maintain = (View)view.findViewById(R.id.view_maintain);

        view_insurance = (View)view.findViewById(R.id.view_insurance);

//        (lin_order_all = (LinearLayout)view.findViewById(R.id.lin_order_all)).setOnClickListener(mClickListener);

        (lin_order_refuel = (LinearLayout)view.findViewById(R.id.lin_order_refuel)).setOnClickListener(mClickListener);

        (lin_order_maintain = (LinearLayout)view.findViewById(R.id.lin_order_maintain)).setOnClickListener(mClickListener);

        (lin_order_insurance = (LinearLayout)view.findViewById(R.id.lin_order_insurance)).setOnClickListener(mClickListener);

        (tv_time = (TextView) view.findViewById(R.id.tv_time)).setOnClickListener(this);

        tv_time.setText(simpleDateFormat.format(calendar.getTime()));

        (iv_left = (ImageView)view. findViewById(R.id.iv_left)).setOnClickListener(this);

        (iv_right = (ImageView) view.findViewById(R.id.iv_right)).setOnClickListener(this);

        myTimePopup = new MyTimePopup(getActivity(), null, new Date(), MyTimePopup.Type.YEAR_MONTH) {

            @Override
            public void onSubmit(Date date) {

                tv_time.setText(simpleDateFormat.format(date));

                time = simpleDateFormat.format(date);

                calendar.setTime(date);

                refresh();

            }

        };

        lin_order_refuel.performClick();

        return view;

    }


    @Override
    public void onClick(View v) {

        switch (v.getId()){

            case R.id.tv_time:

                myTimePopup.show(getActivity().getWindow().getDecorView());

                break;

            case R.id.iv_left:

                calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);

                time = simpleDateFormat.format(calendar.getTime());

                tv_time.setText(time);

                refresh();

                break;

            case R.id.iv_right:

                calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 1);

                Calendar acalendar = Calendar.getInstance();

//                acalendar.set(Calendar.MONTH, acalendar.get(Calendar.MONTH) + 2);

                if (calendar.getTime().getTime() > acalendar.getTime().getTime()) {

                    calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);

                } else {

                    time = simpleDateFormat.format(calendar.getTime());

                }

                tv_time.setText(time);

                refresh();

                break;

        }

    }

    private View.OnClickListener mClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

            initFrg(fragmentTransaction);

            switch (v.getId()) {

//                case R.id.lin_order_all:
//
//                    show = 0;
//
//                    view_all.setVisibility(View.VISIBLE);
//
//                    lin_order_all.setBackgroundResource(R.drawable.bian_bottom_c4c4c4_f5f5f5);
//
//                    break;

                case R.id.lin_order_refuel:

                    show = 1;

                    view_refuel.setVisibility(View.VISIBLE);

                    lin_order_refuel.setBackgroundResource(R.drawable.bian_bottom_c4c4c4_f5f5f5);

                    if(orderRefuelFragment == null){

                        fragmentTransaction.add(R.id.order_content,orderRefuelFragment = new OrderRefuelFragment());

                    }else {

                        fragmentTransaction.show(orderRefuelFragment);

                    }

                    break;

                case R.id.lin_order_maintain:

                    show = 2;

                    view_maintain.setVisibility(View.VISIBLE);

                    lin_order_maintain.setBackgroundResource(R.drawable.bian_bottom_c4c4c4_f5f5f5);

                    break;

                case R.id.lin_order_insurance:

                    show = 3;

                    view_insurance.setVisibility(View.VISIBLE);

                    lin_order_insurance.setBackgroundResource(R.drawable.bian_bottom_c4c4c4_f5f5f5);

                    break;


            }

            fragmentTransaction.commitAllowingStateLoss();

            refresh();

        }
    };

    private void hideFragment(FragmentTransaction fragmentTransaction, Fragment fragment) {

        if (fragment != null) {

            fragmentTransaction.hide(fragment);

        }

    }

    private void initFrg(FragmentTransaction fragmentTransaction) {

//        hideFragment(fragmentTransaction, myOrder1Fragment);
//
//        hideFragment(fragmentTransaction, myOrder2Fragment);
//
//        hideFragment(fragmentTransaction, myOrder3Fragment);

        hideFragment(fragmentTransaction, orderRefuelFragment);

//        lin_order_all.setBackgroundResource(R.drawable.bian_bottom_c3c3c3_eeeeee);

        lin_order_refuel.setBackgroundResource(R.drawable.bian_bottom_c3c3c3_eeeeee);

        lin_order_maintain.setBackgroundResource(R.drawable.bian_bottom_c3c3c3_eeeeee);

        lin_order_insurance.setBackgroundResource(R.drawable.bian_bottom_c3c3c3_eeeeee);

//        view_all.setVisibility(View.INVISIBLE);

        view_refuel.setVisibility(View.INVISIBLE);

        view_maintain.setVisibility(View.INVISIBLE);

        view_insurance.setVisibility(View.INVISIBLE);

    }

    public static String getTime() {

        return time;

    }

    private void refresh(){

        switch (show){

            case 0 :

                break;

            case 1 :

                if(orderRefuelRefreshListener != null) {

                    orderRefuelRefreshListener.OrderRefuelRefresh();

                }

                break;

            case 2 :

                break;

            case 3 :



                break;


        }


    }
}
